<template>
<div>
  <div class="margin-bottom" style="margin-top: -10px">
    <el-button size="small" icon="el-icon-refresh" type="text" @click="listPatches" />
    历史补丁数量: <b>{{ patches.length }}</b>

    <el-button class="margin-left" type="text" size="small" @click="cleanNotEffectHistoryPatch">清空无效记录</el-button>
  </div>

  <el-table
    :data="patches"
    border
    stripe
    size="mini"
    :row-class-name="historyTableRowClass"
  >
    <el-table-column
      type="index"
      label="序号"
      width="55"
    />
    <el-table-column
      prop="title"
      label="增量说明"
    />
    <el-table-column
      prop="branch"
      label="分支"
      width="200"
    />
    <el-table-column
      prop="time"
      label="创建时间"
      width="180"
    >
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.time) }}</span>
      </template>

    </el-table-column>
    <el-table-column
      prop="user"
      label="添加增量人"
      width="100"
    />
    <el-table-column
      fixed="right"
      label="操作"
      width="150"
    >
      <template slot-scope="scope">
        <el-button type="text" :disabled="!scope.row.effect" size="small" @click="downloadHistoryPatch(scope.row.filePath,scope.row.title)">下载</el-button>
        <el-button type="text" size="small" @click="deleteHistoryPatch(scope.row.filePath)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</div>
</template>

<script>
import vcs from '@/api/vcs'
import core from '@/api/core'

import {parseTime} from '@/utils'

export default {
  name: 'patchHistory',
  props: {
    group: String,
    repository: String
  },
  data(){
    return {
      patches: []
    }
  },
  methods: {
    parseTime,
    listPatches(){
      if (this.group && this.repository){
        vcs.patch.historyPatch(this.group,this.repository).then(res => {
          this.patches = res.data
        })
      }
    },
    downloadHistoryPatch(filepath, title) {
      core.fileDownload(filepath, title + '.zip')
    },
    deleteHistoryPatch(filepath) {
      vcs.patch.deleteHistoryPatch(filepath).then(res => {
        this.listPatches()
      })
    },
    cleanNotEffectHistoryPatch() {
      vcs.patch.cleanNotEffectHistoryPatch().then(res => {
        this.listPatches()
      })
    },
    historyTableRowClass({ row, rowIndex }) {
      if (!row.effect) {
        return 'error-row'
      }
      return ''
    },
  }
}
</script>

<style scoped>
/deep/.el-table .error-row {
  color: orangered;
}
</style>
